package com.example.sangong.controller;

import com.example.sangong.http.Result;
import com.example.sangong.req.attachment.AttachmentUrlReq;
import com.example.sangong.resp.AttachmentUploadRsp;
import com.example.sangong.resp.AttachmentUrlResp;
import com.example.sangong.service.AttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "文件上传")
@RestController
@RequestMapping("/attachment")
public class AttachmentController {

    @Autowired
    private AttachmentService attachmentService;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/upload")
    @ResponseBody
    @ApiOperation("上传文件")
    public Result<AttachmentUploadRsp> upload(@ApiParam(value = "附件", required = true) @RequestParam(name = "file") MultipartFile file) {
        return Result.success(attachmentService.uploadFile(file));
    }

    @PostMapping(value = "/batchUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    @ApiOperation("批量上传文件")
    public Result<List<AttachmentUploadRsp>> batchUpload(@ApiParam(value = "多个附件", required = true) @RequestPart(name = "files") List<MultipartFile> files) {
        return Result.success(attachmentService.batchUpload(files));
    }

    @PostMapping(value = "/getAttachmentUrls")
    @ResponseBody
    @ApiOperation("获取文件访问地址")
    public Result<List<AttachmentUrlResp>> getAttachmentUrls(@RequestBody AttachmentUrlReq attachmentUrlReq) {
        return Result.success(attachmentService.getAttachmentUrls(attachmentUrlReq));
    }
    /*
     *//**
     * 下载文件 根据文件名
     * @param fileName
     * @param response
     *//*
    @GetMapping("/v1/get/download")
    public void download(@RequestParam(name = "fileName") String fileName,
                         HttpServletResponse response){
        try {
            minioUtils.fileDownload(fileName,bucketName,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    *//**
     * 下载文件 根据byte数组
     * @param
     *//*
    @GetMapping("/v1/get/downloadByte")
    public void downloadByte(String fileName){
        try {
            minioUtils.byteDownload(filePath,"word-file");
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    *//**
     * 通过文件名删除文件
     * @param fileName
     * @return
     *//*
    @PostMapping("/v1/minio/delete")
    @ResponseBody
    public JSONObject deleteByName(String fileName){
        JSONObject jso = new JSONObject();
        int res = minioUtils.removeFile(bucketName, fileName);
        if (res!=0){
            jso.put("code",5000);
            jso.put("msg","删除失败");
        }
        jso.put("code",2000);
        jso.put("msg","删除成功");
        return jso;
    }*/

}
